package com.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blog.common.Result;
import com.blog.dto.UserDto;
import com.blog.pojo.Blog;
import com.blog.pojo.User;
import com.blog.service.BlogService;
import com.blog.service.SendMailService;
import com.blog.service.UserService;
import com.blog.mapper.UserMapper;
import com.blog.util.ValidateCodeUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author DELL
* @description 针对表【t_user(用户)】的数据库操作Service实现
* @createDate 2022-07-10 15:20:53
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    private BlogService blogService;

    @Autowired
    private SendMailService sendMailService;

    @Override
    public Page page(int page, int pageSize, String username, String gender) {
        Page<User> userPage = new Page<>(page,pageSize);
        Page<UserDto> userDtoPage = new Page<>();
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.like(username!=null,User::getUsername,username);
        userLambdaQueryWrapper.eq(gender!=null,User::getGender,gender);
        //只构造未逻辑删除的
        userLambdaQueryWrapper.eq(User::getIsDelete,0);
        userLambdaQueryWrapper.orderByDesc(User::getUpdateTime);
        //userPage
        this.page(userPage,userLambdaQueryWrapper);
        //将分页对象拷贝给Dto分页对象
        BeanUtils.copyProperties(userPage,userDtoPage,"records");
        //设置DtoPage的records
        List<User> userPageRecords = userPage.getRecords();
        List<UserDto> userDtoList = userPageRecords.stream().map((item) -> {
            UserDto userDto = new UserDto();
            userDto.setUserId(item.getId());
            userDto.setUsername(item.getUsername());
            userDto.setUserAccount(item.getUserAccount());
            userDto.setAvatarUrl(item.getAvatarUrl());
            userDto.setGender(item.getGender());
            userDto.setCreateTime(item.getCreateTime());
            userDto.setUpdateTime(item.getUpdateTime());
            userDto.setUserStatus(item.getUserStatus());
            return userDto;
        }).collect(Collectors.toList());
        //设置records
        userDtoPage.setRecords(userDtoList);
        return userDtoPage;
    }

    @Override
    @Transactional
    public boolean deleteUser(Long userId) {
        LambdaQueryWrapper<Blog> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(Blog::getUserId,userId);
        List<Blog> blogList = blogService.list(userLambdaQueryWrapper);
        if (blogList.size()==0){
            this.removeById(userId);
            return true;
        }else {
            return false;
        }
    }

    /**
     * 发送验证码
     * @param userAccount
     * @param hashMap
     * @return String
     */
    @Override
    public String sendUserCode(String userAccount, HashMap hashMap) {
        if (StringUtils.isNotEmpty(userAccount)) {
            //生成随机4位验证码字符串
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            sendMailService.sendUserMail(userAccount, code);
            //设置验证码到map中，用于后续验证
            hashMap.put(userAccount, code);
            return "验证码已发送!";
        }
        return "发送失败!";
    }
}




